import dayjs from 'dayjs';

/**
 * 返回当前今天现在年月日 时分秒可选
 * @param {boolean} [hour=true] 是否返回时分秒
 */
export const getCurrentTime = (hour = true) => {
	return hour ? dayjs().format('YYYY-MM-DD HH:mm:ss') : dayjs().format('YYYY-MM-DD');
};

/**
 * 返回当前月的第一天 时分秒可选
 * @param {boolean} [hour=true] 是否返回时分秒
 */
export const getCurrentMonthFirst = (hour = true) => {
	return hour ? dayjs().startOf('month').format('YYYY-MM-DD HH:mm:ss') : dayjs().startOf('month').format('YYYY-MM-DD');
};

/**
 * @description 格式化时间
 * @param {*} timestamp
 * @returns
 */
export const formatTimestamp = (timestamp) => {
	const date = new Date(timestamp * 1000);
	const year = date.getFullYear();
	const month = date.getMonth() + 1;
	const day = date.getDate();
	const hours = date.getHours();
	const minutes = date.getMinutes();
	const seconds = date.getSeconds();
	const pad = (num) => (num < 10 ? '0' + num : num);
	return `${year}-${pad(month)}-${pad(day)} ${pad(hours)}:${pad(minutes)}:${pad(seconds)}`;
};

// 时间戳格式化时间
export const formatDate = (timestamp) => {
	const date = new Date(timestamp * 1000);
	const year = date.getFullYear();
	const month = date.getMonth() + 1;
	const day = date.getDate();
	const hours = date.getHours();
	const minutes = date.getMinutes();
	const seconds = date.getSeconds();
	const pad = (num) => (num < 10 ? '0' + num : num);
	return `${year}-${pad(month)}-${pad(day)}`;
};

export const addDay = (days) => {
	const date = new Date();
	date.setDate(date.getDate() + days);
	const year = date.getFullYear();
	const month = date.getMonth() + 1;
	const day = date.getDate();
	const pad = (num) => (num < 10 ? '0' + num : num);
	return `${year}-${pad(month)}-${pad(day)}`;
};
